package org.easyexcel.Test;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.easyexcel.DatalListener.EasyExceGeneralDatalListener;
import org.easyexcel.Pojo.MyEntity;
import org.easyexcel.Server.DataExport;
import org.easyexcel.Dao.JdbcBatchDao;
import org.easyexcel.Utools.TestFileUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author DPF
 */
@SpringBootTest
public class TestExcel {
    @Autowired
    private JdbcBatchDao myService;
    @Autowired
    private DataExport dataExport;


    @Test
    public void testExcel3() {

        String fileName = (String) TestFileUtil.getPath();
        EasyExcel.read(fileName, MyEntity.class, new ReadListener<MyEntity>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 100;
            /**
             *临时存储
             */
            private List<MyEntity> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

            @Override
            public void invoke(MyEntity data, AnalysisContext context) {
                cachedDataList.add(data);
                if (cachedDataList.size() >= BATCH_COUNT) {
                    saveData();
                    // 存储完成清理 list
                    cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
                cachedDataList.forEach(item -> System.out.println(item.toString()));
                myService.batchInsertUsers(cachedDataList);
            }
        }).sheet().doRead();
    }

    @Test
    public void testExcel5() {
        // 写法3：
        String fileName = (String) TestFileUtil.getPath();
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(fileName, MyEntity.class, new EasyExceGeneralDatalListener()).sheet().doRead();
    }
    @Test
    public void testExcel6() {
        dataExport.dataExport300w();
    }
//    @Test
//    public void test7() {
//        Stream<Path> list = null;
//        try {
//            list = Files.walk(Paths.get("D:\\"));
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        list.forEach(System.out::println);
//
//    }
    @Test
    public void test7() {
        MyEntity myEntity = new MyEntity();
        Optional<MyEntity> optional = Optional.ofNullable(myEntity);
        optional.ifPresent(item -> System.out.println(item.toString()));


    }
    @Test
    public void test8() {
        Stream<Integer> in = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
       in.parallel().filter(item -> item > 0).peek(item -> System.out.println("peek:" + Thread.currentThread().getName()))
               .forEach(System.out::println);
    }
    @Test
    public void test9() {
        Class<MyEntity> aClass = MyEntity.class;
//        Constructor<?>[] constructors = aClass.getConstructors();
//        for (Constructor<?> constructor : constructors) {
//            System.out.println(constructor.getName());
//        }
//        Method[] methods = aClass.getMethods();
//        for (Method method : methods) {
//            System.out.println(method.getName());
//        }
        Field[] fields = aClass.getFields();

        for (Field field : fields) {
            field.setAccessible(true);
            System.out.println(field.getName());
        }
    }
    @Test
    public  void test10() {
        Class<?>[] interfaces = ArrayList.class.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface);
        }
    }

}


